(function($) {

	function treegrid_selectRow($grid, rows) {
		for ( var index in rows) {
			var row = rows[index];
			if (row.selected) {
				$grid.datagrid('selectRow', row.id);
			}
			treegrid_selectRow($grid, row.children);
		}
	}

	$.extend($.fn.treegrid.defaults, {
		onAjaxSuccess : function(data) {
		},
		onAjaxError : function() {
		},
		'loader' : function(_122, _123, _124) {
			var $grid = $(this);
			var opts = $grid.treegrid("options");
			if (!opts.url) {
				return false;
			}
			$.ajax({
				type : opts.method,
				url : opts.url,
				data : _122,
				dataType : "json",
				success : function(data) {
					_123(data);
					opts.onAjaxSuccess.call($grid, data);
				},
				error : function() {
					_124.apply(this, arguments);
					opts.onAjaxError.call($grid);
				}
			});
		},
	});

	$.extend($.fn.treegrid.methods, {
		'initSelected' : function(jq) {
			jq.each(function() {
				var $grid = $(this);
				$grid.datagrid('unselectAll');
				var rows = $grid.data('treegrid').data;
				treegrid_selectRow($grid, rows);
			});
		},
		'checkRows' : function(jq, rows) {
			jq.each(function() {
				var $grid = $(this);
				for ( var index in rows) {
					$grid.datagrid('checkRow', rows[index].id);
				}
			});
		},
		'checkUrl' : function(jq, param) {
			jq.each(function() {
				var $grid = $(this);
				$.getJSON(param.url, function(rows) {
					$.fn.treegrid.methods.checkRows(jq, rows);
				});
			});
		},
		'selectRows' : function(jq, rows) {
			jq.each(function() {
				var $grid = $(this);
				for ( var index in rows) {
					$grid.treegrid('selectRow', rows[index].id);
				}
			});
		},
		'selectUrl' : function(jq, param) {
			jq.each(function() {
				var $grid = $(this);
				$.getJSON(param.url, function(rows) {
					$.fn.treegrid.methods.selectRows(jq, rows);
				});
			});
		},
	});

	$.extend($.fn.datagrid.defaults, {
		onAjaxSuccess : function(data) {
		},
		onAjaxError : function() {
		},
		'loader' : function(_248, _249, _24a) {
			var $grid = $(this);
			var opts = $grid.datagrid("options");
			if (!opts.url) {
				return false;
			}
			$.ajax({
				type : opts.method,
				url : opts.url,
				data : _248,
				dataType : "json",
				success : function(data) {
					_249(data);
					opts.onAjaxSuccess.call($grid, data);
				},
				error : function() {
					_24a.apply(this, arguments);
					opts.onAjaxError.call($grid);
				}
			});
		}
	});

	$.extend($.fn.datagrid.methods, {
		'initSelected' : function(jq) {
			jq.each(function() {
				var $grid = $(this);
				$grid.datagrid('unselectAll');
				var rows = $grid.data('datagrid').data;
				for ( var index in rows) {
					var row = rows[index];
					if (row.selected) {
						$grid.datagrid('selectRecord', row.id);
					}
				}
			});
		},
		'checkRecord' : function(jq, row) {
			jq.each(function() {
				var $grid = $(this);
				$grid.datagrid('checkRow', $grid.datagrid('getRowIndex', row));
			});
		},
		'checkRecords' : function(jq, rows) {
			jq.each(function() {
				var $grid = $(this);
				for ( var index in rows) {
					$.fn.datagrid.methods.checkRecord(jq, rows[index]);
				}
			});
		},
		'checkUrl' : function(jq, param) {
			jq.each(function() {
				$grid = $(this);
				$.getJSON(param.url, function(rows) {
					$.fn.datagrid.methods.checkRecords(jq, rows);
				});
			});
		},
		'selectRecords' : function(jq, rows) {
			jq.each(function() {
				var $grid = $(this);
				for ( var index in rows) {
					$grid.datagrid('selectRecord', rows[index].id);
				}
			});
		},
		'selectUrl' : function(jq, param) {
			jq.each(function() {
				var $grid = $(this);
				$.getJSON(param.url, function(rows) {
					$.fn.datagrid.methods.selectRecords(jq, rows);
				});
			});
		},
		'dialog.create' : function(jq, operation) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				if (operation == undefined) {
					operation = 'create';
				}
				var opts = $.extend(true, {}, $.crudgrid.defaults, state.options);
				var param = opts.dialog[operation].param ? '&' + opts.dialog[operation].param : '';
				var method = opts.dialog[operation].name;
				var target = opts.dialog.name;
				var href = opts.dialog[operation].href ? opts.dialog[operation].href : opts.dialog.url + '/' + opts.dialog[operation].url;
				var action = opts.dialog[operation].actionHref ? opts.dialog[operation].actionHref : opts.dialog.url + '/'
						+ (opts.dialog[operation].action ? opts.dialog[operation].action : opts.dialog[operation].url);
				$.dialog.open($.extend({}, {
					title : method + target,
					href : href + '?action=' + action + param,
					buttons : [ $.dialog.buttons.save, $.dialog.buttons.reset, $.dialog.buttons.close ]
				}, opts.dialog.events, opts.dialog[operation], {
					href : href + '?action=' + action + param,
					grid : $grid
				}));
			});
		},
		'dialog.create.parent' : function(jq, operation) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				if (operation == undefined) {
					operation = 'create';
				}
				var opts = $.extend(true, {}, $.crudgrid.defaults, state.options);
				var param = opts.dialog[operation].param ? '&' + opts.dialog[operation].param : '';
				var row = $.fn.datagrid.methods.getSelected($grid);
				var method = opts.dialog[operation].name;
				var target = opts.dialog.name;
				var href = opts.dialog[operation].href ? opts.dialog[operation].href : opts.dialog.url + '/' + opts.dialog[operation].url;
				var action = opts.dialog[operation].actionHref ? opts.dialog[operation].actionHref : opts.dialog.url + '/'
						+ (opts.dialog[operation].action ? opts.dialog[operation].action : opts.dialog[operation].url);
				var idField = opts.dialog[operation].idField ? opts.dialog[operation].idField : 'parent';
				param += row == null ? '' : '&' + idField + '=' + row.id;
				$.dialog.open($.extend({}, {
					title : method + target,
					buttons : [ $.dialog.buttons.save, $.dialog.buttons.reset, $.dialog.buttons.close ]
				}, opts.dialog.events, opts.dialog[operation], {
					href : href + '?action=' + action + param,
					grid : $grid
				}));
			});
		},
		'dialog.update' : function(jq, operation) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				if (operation == undefined) {
					operation = 'update';
				}
				var opts = $.extend(true, {}, $.crudgrid.defaults, state.options);
				var param = opts.dialog[operation].param ? '&' + opts.dialog[operation].param : '';
				var row = $.fn.datagrid.methods.getSelected($grid);
				var method = opts.dialog[operation].name;
				var target = opts.dialog.name;
				var href = opts.dialog[operation].href ? opts.dialog[operation].href : opts.dialog.url + '/' + opts.dialog[operation].url;
				var action = opts.dialog[operation].actionHref ? opts.dialog[operation].actionHref : opts.dialog.url + '/'
						+ (opts.dialog[operation].action ? opts.dialog[operation].action : opts.dialog[operation].url);
				var idField = opts.dialog[operation].idField ? opts.dialog[operation].idField : 'id';
				if (row == null) {
					$.messager.alert('提示', '请选择需' + method + '的' + target, 'info');
				} else {
					$.dialog.open($.extend({}, {
						title : method + target,
						buttons : [ $.dialog.buttons.save, $.dialog.buttons.reset, $.dialog.buttons.close ]
					}, opts.dialog.events, opts.dialog[operation], {
						href : href + '?action=' + action + '&' + idField + '=' + row.id + param,
						grid : $grid
					}));
				}
			});
		},
		'dialog.remove' : function(jq, operation) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				if (operation == undefined) {
					operation = 'remove';
				}
				var opts = $.extend(true, {}, $.crudgrid.defaults, state.options);
				var param = opts.dialog[operation].param ? '&' + opts.dialog[operation].param : '';
				var row = $.fn.datagrid.methods.getSelected($grid);
				var method = opts.dialog[operation].name;
				var target = opts.dialog.name;
				var href = opts.dialog[operation].href ? opts.dialog[operation].href : opts.dialog.url + '/' + opts.dialog[operation].url;
				var action = opts.dialog[operation].actionHref ? opts.dialog[operation].actionHref : opts.dialog.url + '/'
						+ (opts.dialog[operation].action ? opts.dialog[operation].action : opts.dialog[operation].url);
				var options = $.extend({}, {
					title : method + target
				}, opts.dialog.events, opts.dialog[operation], {
					grid : $grid
				})
				var title = options.title;
				var msg = opts.dialog[operation].msg ? opts.dialog[operation].msg : '确定' + method + '选中的' + target + '吗?';
				var idField = opts.dialog[operation].idField ? opts.dialog[operation].idField : 'id';
				if (row == null) {
					$.messager.alert('提示', '请选择需要' + method + '的' + target, 'info');
				} else {
					$.messager.confirm(title, msg, function(r) {
						if (r) {
							$.post(href, idField + '=' + row.id + param, function(data) {
								$.ajaxform.callback(data, options);
								options.onSubmitSuccess.call(this, data, options);
							}, 'text');
						}
					});
				}
			});
		},
		'dialog.search' : function(jq, operation) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				if (operation == undefined) {
					operation = 'search';
				}
				var opts = $.extend(true, {}, $.crudgrid.defaults, state.options);
				var method = opts.dialog[operation].name;
				var target = opts.dialog.name;
				var href = opts.dialog[operation].href ? opts.dialog[operation].href : opts.dialog.url + '/' + opts.dialog[operation].url;
				$.dialog.open($.extend({}, {
					title : method + target,
					buttons : [ $.dialog.buttons.search, $.dialog.buttons.reset, $.dialog.buttons.close ]
				}, opts.dialog.events, opts.dialog[operation], {
					href : href,
					grid : $grid,
					queryParams : state.options.queryParams
				}));
			});
		},
		'toolbar.get' : function(jq) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				return $toolbar;
			});
		},
		'toolbar.index.enable' : function(jq, params) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				if (params == undefined) {
					$toolbar.find('a.l-btn').linkbutton('enable');
				} else if ($.type(params) == "number") {
					$toolbar.find('a.l-btn').eq(params).linkbutton('enable');
				} else if ($.type(params) == "array") {
					for ( var index in params) {
						$toolbar.find('a.l-btn').eq(params[index]).linkbutton('enable');
					}
				}
			});
		},
		'toolbar.index.disable' : function(jq, params) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				if (params == undefined) {
					$toolbar.find('a.l-btn').linkbutton('disable');
				} else if ($.type(params) == "number") {
					$toolbar.find('a.l-btn').eq(params).linkbutton('disable');
				} else if ($.type(params) == "array") {
					for ( var index in params) {
						$toolbar.find('a.l-btn').eq(params[index]).linkbutton('disable');
					}
				}
			});
		},
		'toolbar.text.enable' : function(jq, params) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				if (params == undefined) {
					$toolbar.find('a.l-btn').linkbutton('enable');
				} else if ($.type(params) == "string") {
					$toolbar.find('a:has(span.l-btn-text:contains(' + params + '))').linkbutton('enable');
				} else if ($.type(params) == "array") {
					for ( var index in params) {
						$toolbar.find('a:has(span.l-btn-text:contains(' + params[index] + '))').linkbutton('enable');
					}
				}
			});
		},
		'toolbar.text.disable' : function(jq, params) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				if (params == undefined) {
					$toolbar.find('a.l-btn').linkbutton('disable');
				} else if ($.type(params) == "string") {
					$toolbar.find('a:has(span.l-btn-text:contains(' + params + '))').linkbutton('disable');
				} else if ($.type(params) == "array") {
					for ( var index in params) {
						$toolbar.find('a:has(span.l-btn-text:contains(' + params[index] + '))').linkbutton('disable');
					}
				}
			});
		},
		'toolbar.icon.enable' : function(jq, params) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				if (params == undefined) {
					$toolbar.find('a.l-btn').linkbutton('enable');
				} else if ($.type(params) == "string") {
					$toolbar.find('a:has(span.l-btn-icon.' + params + ')').linkbutton('enable');
				} else if ($.type(params) == "array") {
					for ( var index in params) {
						$toolbar.find('a:has(span.l-btn-icon.' + params[index] + ')').linkbutton('enable');
					}
				}
			});
		},
		'toolbar.icon.disable' : function(jq, params) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				if (params == undefined) {
					$toolbar.find('a.l-btn').linkbutton('disable');
				} else if ($.type(params) == "string") {
					$toolbar.find('a:has(span.l-btn-icon.' + params + ')').linkbutton('disable');
				} else if ($.type(params) == "array") {
					for ( var index in params) {
						$toolbar.find('a:has(span.l-btn-icon.' + params[index] + ')').linkbutton('disable');
					}
				}
			});
		},
		'toolbar.append' : function(jq, element) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				var $tr = $toolbar.find('table tr');
				if ($tr.length == 0) {
					$toolbar.append(element);
				} else {
					$tr.append($('<td></td>').append(element));
				}
			});
		},
		'toolbar.prepend' : function(jq, element) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				var $tr = $toolbar.find('table tr');
				if ($tr.length == 0) {
					$toolbar.prepend(element);
				} else {
					$tr.prepend($('<td></td>').append(element));
				}
			});
		},
		'toolbar.insert' : function(jq, param) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = state.options;
				var $toolbar = state.panel.find('div.datagrid-toolbar');
				var $tr = $toolbar.find('table tr');
				if ($tr.length == 0) {
					$toolbar.children(':eq(' + param.index + ')').before(param.target);
				} else {
					$tr.children(':eq(' + param.index + ')').before($('<td></td>').append(param.target));
				}
			});
		},
		'filter.init' : function(jq, param) {
			jq.each(function() {
				var $grid = $(this);
				$grid.treegrid('enableFilter')
				var state = $grid.data('datagrid');
				var opts = $.extend(true, {}, $.crudgrid.defaults.filter, state.options.filter);
				var $toolbar;
				if (opts.position == 'bottom') {
					$toolbar = state.panel.find('div.datagrid-pager');
				} else {
					$toolbar = state.panel.find('div.datagrid-toolbar');
				}
				var $tr = $toolbar.find('table tr');
				var $input = $('<input />');
				var $textbox = $toolbar.find('span.textbox');
				if ($textbox.length > 0) {
					return;
				}
				if ($tr.length == 0) {
					$toolbar.append($input);
				} else {
					$tr.append($('<td></td>').append($input));
				}
				$input.textbox(opts).textbox('textbox').on('keyup', function(e) {
					$grid.treegrid('removeFilterRule');
					for (var i = 0; i < opts.rules.length; i++) {
						opts.rules[i].value = $(this).val();
						$grid.treegrid('addFilterRule', opts.rules[i]);
					}
					$grid.treegrid('doFilter');
				});
			});
		},
		'search.init' : function(jq, param) {
			jq.each(function() {
				var $grid = $(this);
				var state = $grid.data('datagrid');
				var opts = $.extend(true, {}, $.crudgrid.defaults.search, state.options.search);
				var $toolbar;
				if (opts.position == 'bottom') {
					$toolbar = state.panel.find('div.datagrid-pager');
				} else {
					$toolbar = state.panel.find('div.datagrid-toolbar');
				}
				var $tr = $toolbar.find('table tr');
				var $input = $('<input />');
				var $textbox = $toolbar.find('span.textbox.searchbox');
				if ($textbox.length > 0) {
					return;
				}
				if ($tr.length == 0) {
					$toolbar.append($input);
				} else {
					$tr.append($('<td></td>').append($input));
				}
				$input.searchbox({
					searcher : function(value, name) {
						if (value == '') {
							$.crudgrid.gridMethod($grid, 'load', {});
						} else {
							$.crudgrid.gridMethod($grid, 'load', {
								'searchKey' : value
							});
						}
					},
					prompt : '关键字'
				});
			});
		},
		'linkgrid.load' : function(jq, param) {
			jq.each(function() {
				var $grid = $(this);
				var rows = $.crudgrid.gridMethod($grid, 'getSelections');
				var queryParam = [];
				for (var i = 0; i < rows.length; i++) {
					queryParam.push(rows[i].id);
				}
				var $linkgrid = param.linkgrid;
				$linkgrid.datagrid('clearSelections');
				if (queryParam.length > 0) {
					var p = {};
					p[param.field] = queryParam.join(',');
					$.crudgrid.gridMethod($linkgrid, 'load', $.extend(true, {}, p, param.queryParam));
				}
			});
		}
	});

	$.extend({
		'crudgrid' : {
			'getGrid' : function(gridButton) {
				return $(gridButton).parent().closest('div.datagrid-wrap').children('div.datagrid-view').children('table.datagrid-f');
			},
			'create' : function() {
				$.crudgrid.getGrid(this).datagrid('dialog.create');
			},
			'createByParent' : function() {
				$.crudgrid.getGrid(this).datagrid('dialog.create.parent');
			},
			'update' : function() {
				$.crudgrid.getGrid(this).datagrid('dialog.update');
			},
			'remove' : function() {
				$.crudgrid.getGrid(this).datagrid('dialog.remove');
			},
			'load' : function() {
				$.crudgrid.gridMethod($.crudgrid.getGrid(this), 'load');
			},
			'reload' : function() {
				$.crudgrid.gridMethod($.crudgrid.getGrid(this), 'reload');
			},
			'search' : function() {
				$.crudgrid.getGrid(this).datagrid('dialog.search');
			}
		}
	});

	$.extend($.crudgrid, {
		'isTreegrid' : function(target) {
			if ($(target).data('treegrid')) {
				return true;
			} else {
				return false;
			}
		},
		'gridMethod' : function($grid, method, param) {
			if ($grid.data('treegrid')) {
				return $grid.treegrid(method, param);
			} else {
				return $grid.datagrid(method, param);
			}
		},
		'grid' : function($grid, param) {
			if ($grid.data('treegrid')) {
				return $grid.treegrid(param);
			} else {
				return $grid.datagrid(param);
			}
		},
		'defaults' : {
			'dialog' : {
				'url' : '',
				'name' : '数据',
				'events' : {
					'onSubmitSuccess' : function(data, options) {
						$.dialog.close();
						var $grid = options.grid;
						$.crudgrid.gridMethod($grid, 'reload');
						$.crudgrid.gridMethod($grid, 'clearSelections');
					}
				},
				'create' : {
					'name' : '添加',
					'url' : 'create',
					'action' : 'create',
					'width' : 400,
					'height' : 200,
					'idField' : 'parent'
				},
				'update' : {
					'name' : '编辑',
					'url' : 'update',
					'action' : 'update',
					'width' : 400,
					'height' : 200,
					'idField' : 'id'
				},
				'remove' : {
					'name' : '删除',
					'url' : 'delete',
					'action' : 'delete',
					'msg' : '',
					'idField' : 'id'
				},
				'search' : {
					'name' : '查询',
					'url' : 'search'
				}
			},
			'filter' : {
				'prompt' : '名称',
				'position' : 'top',
				'rules' : [ {
					field : 'name',
					op : 'pinyinContains',
					value : ''
				} ]
			},
			'search' : {
				'prompt' : '名称',
				'position' : 'top'
			}
		},
		'buttons' : {
			'create' : {
				text : '添加',
				iconCls : 'icon-add',
				handler : $.crudgrid.create
			},
			'createByParent' : {
				text : '添加',
				iconCls : 'icon-add',
				handler : $.crudgrid.createByParent
			},
			'update' : {
				text : '编辑',
				iconCls : 'icon-edit',
				handler : $.crudgrid.update
			},
			'remove' : {
				text : '删除',
				iconCls : 'icon-trash',
				handler : $.crudgrid.remove
			},
			'refresh' : {
				text : '刷新',
				iconCls : 'icon-reload',
				handler : $.crudgrid.load
			},
			'search' : {
				iconCls : 'icon-search',
				text : '查询',
				handler : $.crudgrid.search
			},
			'clearSearch' : {
				iconCls : 'icon-error',
				text : '清除查询',
				handler : function() {
					$.crudgrid.getGrid(this).datagrid('load', {});
				}
			}
		}
	});

	$.extend($.crudgrid, {
		toolbar : [ $.crudgrid.buttons.create, $.crudgrid.buttons.update, $.crudgrid.buttons.remove, '-', $.crudgrid.buttons.refresh ]
	});

	$.fn.datagrid.defaults.view = $.extend({}, $.fn.datagrid.defaults.view, {
		renderRow : function(_211, _212, _213, _214, _215) {
			var opts = $.data(_211, "datagrid").options;
			var cc = [];
			if (_213 && opts.rownumbers) {
				var _216 = _214 + 1;
				if (opts.pagination) {
					_216 += (opts.pageNumber - 1) * opts.pageSize;
				}
				cc.push("<td class=\"datagrid-td-rownumber\"><div class=\"datagrid-cell-rownumber\">" + _216 + "</div></td>");
			}
			for (var i = 0; i < _212.length; i++) {
				var _217 = _212[i];
				var col = $(_211).datagrid("getColumnOption", _217);
				if (col) {
					var _218 = _215[_217];
					var css = col.styler ? (col.styler(_218, _215, _214) || "") : "";
					var cs = this.getStyleValue(css);
					var cls = cs.c ? "class=\"" + cs.c + "\"" : "";
					var _219 = col.hidden ? "style=\"display:none;" + cs.s + "\"" : (cs.s ? "style=\"" + cs.s + "\"" : "");
					cc.push("<td field=\"" + _217 + "\" " + cls + " " + _219 + ">");
					var _219 = "";
					if (!col.checkbox) {
						if (col.align) {
							_219 += "text-align:" + col.align + ";";
						}
						if (!opts.nowrap) {
							_219 += "white-space:normal;height:auto;";
						} else {
							if (opts.autoRowHeight) {
								_219 += "height:auto;";
							}
						}
					}
					cc.push("<div style=\"" + _219 + "\" ");
					cc.push(col.checkbox ? "class=\"datagrid-cell-check\"" : "class=\"datagrid-cell " + col.cellClass + "\"");
					cc.push(">");
					if (col.checkbox) {
						cc.push("<input type=\"checkbox\" " + (_215.checked ? "checked=\"checked\"" : ""));
						cc.push(" name=\"" + _217 + "\" value=\"" + (_218 != undefined ? _218 : _215[opts.idField]) + "\">");
					} else {
						if (col.formatter) {
							cc.push(col.formatter(_218, _215, _214));
						} else {
							cc.push(_218);
						}
					}
					cc.push("</div>");
					cc.push("</td>");
				}
			}
			return cc.join("");
		}
	});

	$.fn.datagrid.defaults.operators = $.extend({}, $.fn.datagrid.defaults.operators, {
		pinyinContains : {
			text : 'PinyinContains',
			isMatch : function(source, value) {
				source = String(source).toUpperCase();
				value = String(value).toUpperCase();
				pinyinSource = pinyin.getCamelChars(source);
				return source.indexOf(value) >= 0 || pinyinSource.indexOf(value) >= 0;
			}
		}
	});
	$.fn.treegrid.defaults.operators = $.fn.datagrid.defaults.operators;

	$.fn.treegrid.defaults.view = $.extend({}, $.fn.treegrid.defaults.view, {
		renderRow : function(_f6, _f7, _f8, _f9, row) {
			var _fa = $.data(_f6, "treegrid");
			var _fb = _fa.options;
			var cc = [];
			if (_f8 && _fb.rownumbers) {
				cc.push("<td class=\"datagrid-td-rownumber\"><div class=\"datagrid-cell-rownumber\">0</div></td>");
			}
			for (var i = 0; i < _f7.length; i++) {
				var _fc = _f7[i];
				var col = $(_f6).datagrid("getColumnOption", _fc);
				if (col) {
					var css = col.styler ? (col.styler(row[_fc], row) || "") : "";
					var cs = this.getStyleValue(css);
					var cls = cs.c ? "class=\"" + cs.c + "\"" : "";
					var _fd = col.hidden ? "style=\"display:none;" + cs.s + "\"" : (cs.s ? "style=\"" + cs.s + "\"" : "");
					cc.push("<td field=\"" + _fc + "\" " + cls + " " + _fd + ">");
					var _fd = "";
					if (!col.checkbox) {
						if (col.align) {
							_fd += "text-align:" + col.align + ";";
						}
						if (!_fb.nowrap) {
							_fd += "white-space:normal;height:auto;";
						} else {
							if (_fb.autoRowHeight) {
								_fd += "height:auto;";
							}
						}
					}
					cc.push("<div style=\"" + _fd + "\" ");
					if (col.checkbox) {
						cc.push("class=\"datagrid-cell-check ");
					} else {
						cc.push("class=\"datagrid-cell " + col.cellClass);
					}
					cc.push("\">");
					if (col.checkbox) {
						if (row.checked) {
							cc.push("<input type=\"checkbox\" checked=\"checked\"");
						} else {
							cc.push("<input type=\"checkbox\"");
						}
						cc.push(" name=\"" + _fc + "\" value=\"" + (row[_fc] != undefined ? row[_fc] : row[opts.idField]) + "\">");
					} else {
						var val = null;
						if (col.formatter) {
							val = col.formatter(row[_fc], row);
						} else {
							val = row[_fc];
						}
						if (_fc == _fb.treeField) {
							for (var j = 0; j < _f9; j++) {
								cc.push("<span class=\"tree-indent\"></span>");
							}
							if (row.state == "closed") {
								cc.push("<span class=\"tree-hit tree-collapsed\"></span>");
								cc.push("<span class=\"tree-icon tree-folder " + (_fb.treeFolderCls ? _fb.treeFolderCls : "") + " " + (row.iconCls ? row.iconCls : "") + "\"></span>");
							} else {
								if (row.children && row.children.length) {
									cc.push("<span class=\"tree-hit tree-expanded\"></span>");
									cc.push("<span class=\"tree-icon tree-folder tree-folder-open " + (_fb.treeFolderOpenCls ? _fb.treeFolderOpenCls : "") + " " + (row.iconCls ? row.iconCls : "")
											+ "\"></span>");
								} else {
									cc.push("<span class=\"tree-indent\"></span>");
									cc.push("<span class=\"tree-icon tree-file " + (_fb.treeFileCls ? _fb.treeFileCls : "") + " " + (row.iconCls ? row.iconCls : "") + "\"></span>");
								}
							}
							if (this.hasCheckbox(_f6, row)) {
								var _fe = 0;
								var _ff = $.easyui.getArrayItem(_fa.checkedRows, _fb.idField, row[_fb.idField]);
								if (_ff) {
									_fe = _ff.checkState == "checked" ? 1 : 2;
								} else {
									var prow = $.easyui.getArrayItem(_fa.checkedRows, _fb.idField, row._parentId);
									if (prow && prow.checkState == "checked" && _fb.cascadeCheck) {
										_fe = 1;
										row.checked = true;
										$.easyui.addArrayItem(_fa.checkedRows, _fb.idField, row);
									} else {
										if (row.checked) {
											$.easyui.addArrayItem(_fa.tmpIds, row[_fb.idField]);
										}
									}
									row.checkState = _fe ? "checked" : "unchecked";
								}
								cc.push("<span class=\"tree-checkbox tree-checkbox" + _fe + "\"></span>");
							} else {
								row.checkState = undefined;
								row.checked = undefined;
							}
							cc.push("<span class=\"tree-title\">" + val + "</span>");
						} else {
							cc.push(val);
						}
					}
					cc.push("</div>");
					cc.push("</td>");
				}
			}
			return cc.join("");
		}
	});

})(jQuery);
